﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *
 **/

using System.Windows.Input;
using Peter.Common;
using Peter.Common.Interfaces;

namespace Peter.Logic
{
   /// <summary>
   /// A base class for a tool window that supports edit commands.
   /// </summary>
   public abstract class EditablePeterToolWindow : PeterToolWindow, IEditable
   {
      private bool m_NeedsSaving;
      private RelayCommand m_CmdEdit;

      /// <summary>
      /// Gets if the editable needs saving.
      /// </summary>
      public virtual bool NeedsSaving
      {
         get { return this.m_NeedsSaving; }
         protected set
         {
            this.m_NeedsSaving = value;
            this.OnPropertyChanged ("NeedsSaving");
         }
      }

      /// <summary>
      /// Gets the edit command.
      /// </summary>
      public ICommand EditCommand
      {
         get { return this.m_CmdEdit ?? (this.m_CmdEdit = new RelayCommand (this.OnEdit, this.CanEdit)); }
      }

      /// <summary>
      /// Checks whether an edit actions can be performed or not.
      /// </summary>
      /// <param name="obj">Edit action</param>
      /// <returns>True if action can be performed, otherwise false.</returns>
      protected virtual bool CanEdit (object obj)
      {
         var editAction = (EditAction)obj;
         switch (editAction)
         {
            case EditAction.Copy:
            case EditAction.SelectAll:
               return true;
            case EditAction.Save:
               return this.NeedsSaving;
            default:
               return false;
         }
      }

      /// <summary>
      /// Performs the edit action.
      /// </summary>
      /// <param name="obj">Edit action.</param>
      protected virtual void OnEdit (object obj)
      {
      }
   }
}
